package com.maszy.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.maszy.bean.CashRegister;
import com.maszy.db.DBConnection;

public class CashRegisterService {
	CashRegister service=new CashRegister(); 
	private static PreparedStatement pst;
	private static ResultSet rs = null;
	public  int add(CashRegister obj) {
		DBConnection con = new DBConnection();
		int result = 0;
		String sql ="insert into Maszy_CashRegister(Maszy_CashRegister_NO,Maszy_CashRegister_IP,Maszy_DoorStore_Id,Maszy_CashRegister_Status,Maszy_CashRegister_Remark) values(?,?,?,?,?)";
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {			
			pst.setString(1,obj.getCashRegister_NO());
			pst.setString(2, obj.getCashRegister_IP());
			pst.setLong(3, obj.getDoorStore_Id());
			pst.setInt(4, obj.getCashRegister_Status());
			pst.setString(5, obj.getCashRegister_Remark());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return result;
	}
	public int update(CashRegister obj){
		DBConnection con = new DBConnection();
		int i=0;
		String sql="update Maszy_CashRegister set Maszy_CashRegister_NO=?,Maszy_CashRegister_IP=?,Maszy_DoorStore_Id=?,Maszy_CashRegister_Status=?,Maszy_CashRegister_Remark=? where Maszy_CashRegister_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			pst.setString(1,obj.getCashRegister_NO());
			pst.setString(2, obj.getCashRegister_IP());
			pst.setLong(3, obj.getDoorStore_Id());
			pst.setInt(4, obj.getCashRegister_Status());
			pst.setString(5, obj.getCashRegister_Remark());
			pst.setLong(6, obj.getCashRegister_Id());
			i=pst.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}
	public int delete(CashRegister obj){
		DBConnection con = new DBConnection();
		int i=0;
		String sql="delete from  Maszy_CashRegister  where Maszy_CashRegister_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);	
			pst.setLong(1, obj.getCashRegister_Id());
			i=pst.executeUpdate();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;

				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return i;
	}
	public CashRegister queryById(CashRegister obj){
		DBConnection con = new DBConnection();
		String sql="select * from Maszy_CashRegister where Maszy_CashRegister_Id=?";
		try {
			pst = con.getConnection().prepareStatement(sql);	
			pst.setLong(1, obj.getCashRegister_Id());
			rs = pst.executeQuery();
			while (rs.next()) {
				obj=new CashRegister();
				obj.setCashRegister_Id(rs.getLong("Maszy_CashRegister_Id"));
				obj.setCashRegister_NO(rs.getString("Maszy_CashRegister_NO"));
				obj.setCashRegister_IP(rs.getString("Maszy_CashRegister_IP"));
				obj.setDoorStore_Id(rs.getLong("Maszy_DoorStore_Id"));	
				obj.setCashRegister_Status(rs.getInt("Maszy_CashRegister_Status"));
				obj.setCashRegister_Remark(rs.getString("Maszy_CashRegister_Remark"));	
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return obj;
	}
	public List queryAll(){
		List list=new ArrayList();
		DBConnection con = new DBConnection();
		String sql="select Maszy_CashRegister.*,Maszy_DoorStore.* from Maszy_DoorStore,Maszy_CashRegister where Maszy_CashRegister.Maszy_DoorStore_Id=Maszy_DoorStore.Maszy_DoorStore_Id";
		try {
			pst = con.getConnection().prepareStatement(sql);				
			rs = pst.executeQuery();
			   while (rs.next()) {
				    Map<String, Object> map = new HashMap<String, Object>();
				    ResultSetMetaData rsmd = rs.getMetaData();
				    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
				    	if("Maszy_CashRegister_Status".equals(rsmd.getColumnName(i))){
				    		String Maszy_CashRegister_Status=new String();
				    		switch((Integer)rs.getInt(i)){
				    		case 1: Maszy_CashRegister_Status="闲置";break;
				    		case 2:Maszy_CashRegister_Status="正在使用";break;
				    		case 3:Maszy_CashRegister_Status="故障";break;	  
				    		}
				    		map.put("Maszy_CashRegister_Status",Maszy_CashRegister_Status);
				    	}else{
				    		map.put(rsmd.getColumnName(i), rs.getObject(i));
				    	}
				     
				    }
				    list.add(map);
				   }
	
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return list;
	}
	public List<CashRegister> query(){
		List<CashRegister> list=new ArrayList<CashRegister>();
		DBConnection con = new DBConnection();
		String sql="select Maszy_CashRegister.* from Maszy_CashRegister ";
		try {
			pst = con.getConnection().prepareStatement(sql);				
			rs = pst.executeQuery();
			CashRegister cr=new CashRegister();
			   while (rs.next()) {
				   CashRegister  obj=new CashRegister();
					obj.setCashRegister_Id(rs.getLong("Maszy_CashRegister_Id"));
					obj.setCashRegister_NO(rs.getString("Maszy_CashRegister_NO"));
					obj.setCashRegister_IP(rs.getString("Maszy_CashRegister_IP"));
					obj.setDoorStore_Id(rs.getLong("Maszy_DoorStore_Id"));	
					obj.setCashRegister_Status(rs.getInt("Maszy_CashRegister_Status"));
					obj.setCashRegister_Remark(rs.getString("Maszy_CashRegister_Remark"));	
				    list.add(obj);
			   }
	
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (Exception e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		return list;
	}
}
